package cn.ainit.yz.printexplain;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.ainit.butn.proceduce.ButScripParams;
import cn.ainit.common.ReturnObject;
import cn.ainit.sys.ds.store.StoreService;
import cn.ainit.sys.menu.SessionCont;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.printexplain.entity.PrintExplain;
import cn.ainit.yz.printexplain.entity.PrintExplainFM;
import cn.ainit.yz.printexplain.entity.PrintExplainZD;
import cn.ainit.yz.printexplain.entity.PrintExplainZW;

@Controller
@RequestMapping("/printExplain")
public class PrintExplainControllor {
	PrintExplainService printExplainService;
	
	@Resource
	public void setPrintExplainService(PrintExplainService printExplainService) {
		this.printExplainService = printExplainService;
	}
	private StoreService storeService;

	@Resource
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}
	
	
	
	@RequestMapping("/newExecut")
	@ResponseBody
	public ReturnObject newExecut(String peId,String dsId, HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String exId = this.printExplainService.printToexec(peId);
			String keyV = "EX_ID-"+exId;
			returnObject.setInfo(keyV);
			String[] dsIds = dsId.split(";"); 
			DataStore dataStore = storeService.findStore(dsIds[0]);
			returnObject.setDynObj(dataStore);
 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 材料出库（领料出库）
	 * @param peId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-5-13 下午02:17:43
	 */
	@RequestMapping("/cailiaoChuku")
	@ResponseBody
	public ReturnObject clchuku(String peId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String ckId = this.printExplainService.caiLiaoChuku(peId);
			if (ckId != null) {
				returnObject.setMsg("success");
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	
	
	
	
	
	
	/**
	 * 保存印刷说明单基本信息
	 * @param request
	 * @return
	 * @author Xuzq
	 * @date 2014-2-26 下午05:44:12
	 */
	@RequestMapping("/saveBase")
	@ResponseBody
	public ReturnObject saveBase(@RequestBody PrintExplain printExplain, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			if (printExplain.getPeId() != null && (!printExplain.getPeId().equals(""))) {
				//有主键   说明是更新
				if(printExplainService.updateBase(printExplain)){
					returnObject.setMsg("upsuccess");
				}else {
					returnObject.setMsg("upfail");
				}
			}else{
				//没有主键  保存
				String peIdStr = UUID32.newGuid();
				printExplain.setPeId(peIdStr);//设置主键
				if (printExplainService.saveBase(printExplain)) {
					returnObject.setMsg("success");
					returnObject.setInfo(peIdStr);
				}else {
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/***
	 * 保存封面印刷
	 * @param printExplainFMs
	 * @param request
	 * @return
	 * @author Xuzq
	 * @date 2014-2-27 下午02:31:48
	 */
	@RequestMapping("/saveFM")
	@ResponseBody
	public ReturnObject saveFM(@RequestBody List<PrintExplainFM> printExplainFMs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = printExplainService.saveFM(printExplainFMs,peId);
			if(ids != null && ids.size() == printExplainFMs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/***
	 * 保存正文印刷
	 * @param printExplainFMs
	 * @param peId
	 * @param request
	 * @return
	 * @author Xuzq
	 * @date 2014-2-28 下午03:45:47
	 */
	@RequestMapping("/saveZW")
	@ResponseBody
	public ReturnObject saveZW(@RequestBody List<PrintExplainZW> printExplainZWs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = printExplainService.saveZW(printExplainZWs,peId);
			if(ids != null && ids.size() == printExplainZWs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/***
	 * 保存装订项目
	 * @param printExplainZDs
	 * @param peId
	 * @param request
	 * @return
	 * @author Xuzq
	 * @date 2014-2-28 下午03:45:47
	 */
	@RequestMapping("/saveZD")
	@ResponseBody
	public ReturnObject saveZD(@RequestBody List<PrintExplainZD> printExplainZDs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = printExplainService.saveZD(printExplainZDs,peId);
			if(ids != null && ids.size() == printExplainZDs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	
	
	/**
	 * 加载一个印刷单的所有明细
	 * @param ysId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 */
	@RequestMapping("/findItems")
	@ResponseBody
	public ReturnObject findItemsInfo(String ysId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			List<PrintExplainFM> printExplainFMs = this.printExplainService.findFMList(ysId);
			List<PrintExplainZW> printExplainZWs = this.printExplainService.findZWList(ysId);
			List<PrintExplainZD> printExplainZDs = this.printExplainService.findZDList(ysId);
			List list = new ArrayList();
			list.add(printExplainFMs);
			list.add(printExplainZWs);
			list.add(printExplainZDs);
			returnObject.setObjList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	
	
	@RequestMapping("/returnNewBath")
	@ResponseBody
	public String returnNewBath(String xtId, String byc, String config){
		String data=null;
		try {
			data=this.printExplainService.returnNewBath(xtId, byc, config);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	
	@RequestMapping("/keepModifyPrems")
	@ResponseBody
	public String keepModifyPrems(String dsId,String djId,HttpSession session){
		String isMP=null;
		try {
			isMP=printExplainService.keepModifyPrems(dsId,djId,session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isMP;
	}
}
